< previous page page_231 next page >

Page 231
The second term, ((Day1 ) * 3), gives us the number of the first byte for a given day (each day has 3 bytes). Each byte holds 8 hours, so divide by 8 to get the byte index. You have to use the Int function to prevent rounding. For example:
Monday 10:00 AM is day 2, hour 10.
ByteIndex = Int(10/8) + ((2-1) * 3) = 1 + 3 = 4
Which bit corresponds to this time? We found the index of the byte by dividing by 8the bit location is the remainder of this operation. The easiest way to obtain it is by using the mod operator: 10 mod 8 is 2, indicating that the correct bit is bit 2 (the third bit starting from bit 0).
But we don't really need the bit numberwe actually need a byte in which the specified bit is set in order to use an AND or OR operation to clear or set the bit. Since Visual Basic does not have a shift operator, we can use its exponentiation operator as follows:
ByteMask = 2 ^ Int(Hour Mod 8)
Now, I realize that whether this makes sense to you or not depends on how far you are from your high school math. I've found that the easiest way to handle these types of problems is often to write out some sample values and see if they make sense. Use Table S23-1 as a format for your own examples.
HOURDAYINT(HOUR / 8)BYTEINDEXHOUR MOD 82 ^ (HOUR MOD 8)
010001
020301
72037128 = &H80
821401
Table S23-1: Trial values can help you determine if an equation is correct

To set a bit, simply use the OR operator to combine the mask value with the existing value.
To clear a bit, you actually need a mask in which all of the bits are set except for the one you want to clear. You can get this value by using the NOT operator on the mask. You can then use the AND operator to combine the mask with the original value to clear the bit.
The final function to set or clear bit values is shown here:
' Day is 1 - 7
' Hour is 0 - 23

 
< previous page page_231 next page >